package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.*;
import com.ruoyi.background.mapper.AppLotteryRecordMapper;
import com.ruoyi.background.service.IAppGradeRecordService;
import com.ruoyi.background.service.IAppLotteryRecordService;
import com.ruoyi.background.service.IAppWechatUserService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 积分抽奖记录Service业务层处理
 *
 * @author susu
 * @date 2024-08-24
 */
@Service
public class AppLotteryRecordServiceImpl implements IAppLotteryRecordService {
    @Autowired
    private AppLotteryRecordMapper appLotteryRecordMapper;

    @Autowired
    private IAppWechatUserService wechatUserService;

    @Autowired
    private IAppGradeRecordService gradeRecordService;


    /**
     * 查询积分抽奖记录
     *
     * @param id 积分抽奖记录主键
     * @return 积分抽奖记录
     */
    @Override
    public AppLotteryRecord selectAppLotteryRecordById(Long id) {
        return appLotteryRecordMapper.selectAppLotteryRecordById(id);
    }

    /**
     * 查询积分抽奖记录列表
     *
     * @param appLotteryRecord 积分抽奖记录
     * @return 积分抽奖记录
     */
    @Override
    public List<AppLotteryRecord> selectAppLotteryRecordList(AppLotteryRecord appLotteryRecord) {
        return appLotteryRecordMapper.selectAppLotteryRecordList(appLotteryRecord);
    }

    /**
     * 新增积分抽奖记录
     *
     * @param appLotteryRecord 积分抽奖记录
     * @return 结果
     */
    @Override
    public int insertAppLotteryRecord(AppLotteryRecord appLotteryRecord) {
        appLotteryRecord.setCreateTime(DateUtils.getNowDate());
        return appLotteryRecordMapper.insertAppLotteryRecord(appLotteryRecord);
    }

    /**
     * 修改积分抽奖记录
     *
     * @param appLotteryRecord 积分抽奖记录
     * @return 结果
     */
    @Override
    public int updateAppLotteryRecord(AppLotteryRecord appLotteryRecord) {
        appLotteryRecord.setUpdateTime(DateUtils.getNowDate());
        return appLotteryRecordMapper.updateAppLotteryRecord(appLotteryRecord);
    }

    /**
     * 批量删除积分抽奖记录
     *
     * @param ids 需要删除的积分抽奖记录主键
     * @return 结果
     */
    @Override
    public int deleteAppLotteryRecordByIds(Long[] ids) {
        return appLotteryRecordMapper.deleteAppLotteryRecordByIds(ids);
    }

    /**
     * 删除积分抽奖记录信息
     *
     * @param id 积分抽奖记录主键
     * @return 结果
     */
    @Override
    public int deleteAppLotteryRecordById(Long id) {
        return appLotteryRecordMapper.deleteAppLotteryRecordById(id);
    }

    /**
     * 导出积分抽奖记录
     *
     * @param response       响应流
     * @param appGradeRecord 查询条件
     */
    @Override
    public void exportUserLotteryRecord(HttpServletResponse response, AppLotteryRecord appGradeRecord) {
        //中奖名单
        List<AppLotteryRecord> list = this.selectAppLotteryRecordList(appGradeRecord);
        list.sort(Comparator.comparing(AppLotteryRecord::getUserId));
        //按部门分组
        Map<Long, List<AppLotteryRecord>> groupedByDeptId = list.stream()
                .collect(Collectors.groupingBy(AppLotteryRecord::getDeptId));
        // 先创建要导出的表格
        AppLotteryExport export = new AppLotteryExport();
        String t = appGradeRecord.getLotteryIndex() == null ? "全部" : "第" + appGradeRecord.getLotteryIndex() + "轮";
        String t1 = appGradeRecord.getLotteryIndex() == null ? "抽奖全部统计数据" : "第" + appGradeRecord.getLotteryIndex() + "轮抽奖统计数据";
        // 统计数据
        List<AppLotteryStatistics> statisticsList = new ArrayList<>();
        groupedByDeptId.forEach((deptId, lotteryRecords) -> {
            String deptName = lotteryRecords.get(0).getDeptName();
            Integer num = lotteryRecords.size();
            AppLotteryStatistics statistics = new AppLotteryStatistics();
            statistics.setDeptName(deptName);
            statistics.setNum(num);
            statisticsList.add(statistics);

        });
        export.createSheet("合计数据");
        export.writeCountHeadTitle(t1);
        export.writeCountHead();
        export.writeCountData(statisticsList);
        //将分组完成的对象导入excel中
        groupedByDeptId.forEach((deptId, lotteryRecords) -> {
            String deptName = lotteryRecords.get(0).getDeptName();
            export.createSheet(deptName);
            export.writeHeadTitle(deptName + t + "中奖名单，共计" + lotteryRecords.size() + "条中奖记录");
            export.writeHead();
            export.writeListData(lotteryRecords);
        });
        export.excelDownload(response);
    }

    /**
     *
     */
    @Override
    @Transactional
    public void find171416() {
        List<AppLotteryRecord> rows = appLotteryRecordMapper.find171416Rows();
        System.out.println("总计补充记录：" + rows.size());
        for (AppLotteryRecord row : rows) {
            Long userId = row.getUserId();
            //发放积分奖励
            int grade = 300;
            AppWechatUser wechatUser = wechatUserService.selectAppWechatByUserId(userId);
            AppGradeRecord record = new AppGradeRecord();
            record.setUserId(userId);
            record.setGrade(grade);
            record.setType(0);
            record.setBeforeGrade(wechatUser.getGrade());
            record.setCreateTime(DateUtils.getNowDate());
            record.setRemark("9月17号系统积分抽奖活动退回");
            gradeRecordService.insertAppGradeRecord(record);
            AppWechatUser update = new AppWechatUser();
            update.setId(wechatUser.getId());
            update.setGrade(wechatUser.getGrade() + grade);
            wechatUserService.updateAppWechatUser(update);
            System.out.println("补偿员工：" + row.getUserName());
        }

    }
}
